package com.github.microwww.upms.controller;

import com.github.microwww.upms.service.WebappUserService;
import com.github.microwww.upms.vo.WebappUserValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping(value = "/webapp-user")
public class WebappUserController {

    @Autowired()
    WebappUserService webappUserService;

    @GetMapping(value = "/detail")
    public WebappUserValue.Info detailWebappUser(int id) {
        return this.webappUserService.findById(id).map(WebappUserValue.Info::new).orElseGet(null);
    }

    @GetMapping(value = "/list")
    public List<WebappUserValue.Simple> listWebappUser(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        return this.webappUserService.findAll(page, size).stream().map(WebappUserValue.Simple::new).collect(Collectors.toList());
    }
}
